package com.heima.xiyuanmall.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.heima.xiyuanmall.R;
import com.heima.xiyuanmall.sudoku.Pos;
import com.heima.xiyuanmall.sudoku.SudokuAutoSolver;
import com.heima.xiyuanmall.view.SudokuGameView;

import java.util.Random;

public class SudokuGameActivity extends AppCompatActivity {

    private SudokuGameView sudokuGameView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sudoku_game);
        initview();
    }

    Handler handler;
    Runnable runnable = () -> SudokuAutoSolver.INSTANCE.solve(new Pos());

    private void initview() {
        sudokuGameView = findViewById(R.id.sudokugameview);
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                sudokuGameView.refresh((int[][]) msg.obj);

                return false;
            }
        });

        initAutoSolver();
        findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //“显示信息“  点击监听事件
//                sudokuGameView.repeat();     //重新开始
//                sudokuGameView.refresh(new int[][]{});    //刷新
//                sudokuGameView.replay(new int[][]{});        //再来一局
            }
        });
//        handler.postDelayed(runnable, 50);    //循环刷新
    }

    private void initAutoSolver() {
        SudokuAutoSolver.INSTANCE.init(handler);
        SudokuAutoSolver.INSTANCE.start();
    }
}